package vn.orderonline.com.core.business.drink;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vn.orderonline.com.core.mvc.model.DrinkDto;
import vn.orderonline.core.commons.utils.DateUtils;
import vn.orderonline.core.commons.utils.StringUtils;
import vn.orderonline.core.dao.DrinkDao;
import vn.orderonline.core.dao.MenuDrinkDrinkDao;
import vn.orderonline.core.dao.entity.Drink;
import vn.orderonline.core.dao.entity.MenuDrinkDrink;
import vn.orderonline.core.dao.entity.MenuDrinkDrinkId;

@Service
@Transactional
public class DrinkImpl implements IDrink {

	@Autowired
	private DrinkDao drinkDao;
	@Autowired
	MenuDrinkDrinkDao menuDrinkDrinkDao;

	@Override
	public List<DrinkDto> getAllDrinkByResIdAndMenuDrinkId(Integer resId,
			Integer menuDrinkId) {
		List<DrinkDto> list = new ArrayList<>();
		if (resId != null && menuDrinkId != null) {
			List<Object[]> listObj = drinkDao
					.getListDrinkByResIdAndMenuDrinkId(resId, menuDrinkId);
			DrinkDto dto = null;
			if (!listObj.isEmpty()) {
				for (Object[] obj : listObj) {
					dto = new DrinkDto();
					dto.setRestaurantId((Integer) obj[0]);
					dto.setRestaurantName((String) obj[1]);
					dto.setMenuDrinkId((Integer) obj[2]);
					dto.setMenuDrinkName((String) obj[3]);
					Drink drink = (Drink) obj[4];
					dto.setDrinkId(drink.getDrinkId());
					dto.setDrinkName(drink.getDrinkName());
					dto.setDrinkTitle(drink.getDrinkTitle());
					dto.setDrinkImage(drink.getDrinkImage());
					dto.setOldPrice(drink.getOldPrice());
					dto.setNewPrice(drink.getNewPrice());
					dto.setCreateTime(drink.getCreateTime());
					dto.setUpdateTime(drink.getUpdateTime());
					dto.setCreateId(drink.getCreateId());
					dto.setUpdateId(drink.getUpdateId());
					dto.setDrinkStatus(drink.getDrinkStatus());
					dto.setDescription(drink.getDescription());
					list.add(dto);
				}
			}
		}
		return list;
	}

	@Override
	public Boolean checkExistedDrink(Integer menuDrinkId, String drinkName) {
		if (menuDrinkId != null && !StringUtils.isNullOrEmpty(drinkName)) {
			if (drinkDao.checkExistedDrink(menuDrinkId, drinkName)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void addDrink(DrinkDto drink) {
		MenuDrinkDrink mdd = null;
		Drink dr = null;
		if (drink != null) {
			dr = new Drink();
			dr.setDrinkName(drink.getDrinkName());
			dr.setDrinkTitle(drink.getDrinkTitle());
			dr.setDrinkImage(drink.getDrinkImage());
			dr.setOldPrice(drink.getOldPrice());
			dr.setNewPrice(drink.getNewPrice());
			dr.setCreateTime(new Date(DateUtils.now()));
			dr.setCreateId("ZUchihashishui");
			dr.setDescription(drink.getDescription());
			dr.setDrinkStatus(true);
			drinkDao.save(dr);

			mdd = new MenuDrinkDrink(new MenuDrinkDrinkId(
					drink.getMenuDrinkId(), dr.getDrinkId()));
			menuDrinkDrinkDao.save(mdd);
		}

	}

	@Override
	public DrinkDto viewDetail(Integer drinkId) {
		DrinkDto dto = new DrinkDto();
		if (drinkId != null) {
			Object[] obj = drinkDao.getDrink(drinkId);
			if (obj != null) {
				dto.setRestaurantId((Integer) obj[0]);
				dto.setRestaurantName((String) obj[1]);
				dto.setMenuDrinkId((Integer) obj[2]);
				dto.setMenuDrinkName((String) obj[3]);
				Drink drink = (Drink) obj[4];
				dto.setDrinkId(drink.getDrinkId());
				dto.setDrinkName(drink.getDrinkName());
				dto.setDrinkTitle(drink.getDrinkTitle());
				dto.setDrinkImage(drink.getDrinkImage());
				dto.setOldPrice(drink.getOldPrice());
				dto.setNewPrice(drink.getNewPrice());
				dto.setCreateTime(drink.getCreateTime());
				dto.setUpdateTime(drink.getUpdateTime());
				dto.setCreateId(drink.getCreateId());
				dto.setUpdateId(drink.getUpdateId());
				dto.setDrinkStatus(drink.getDrinkStatus());
				dto.setDescription(drink.getDescription());
			}
		}
		return dto;
	}

	@Override
	public void deleteDrink(Integer menuDrinkId, Integer drinkId) {
		if (menuDrinkId != null && drinkId != null) {
			MenuDrinkDrink mdd = menuDrinkDrinkDao.find(new MenuDrinkDrinkId(
					menuDrinkId, drinkId));
			menuDrinkDrinkDao.delete(mdd);

			Drink drink = drinkDao.find(drinkId);
			drinkDao.delete(drink);
		}
	}

	@Override
	public void updateDrink(DrinkDto drinkDto) {
		if (drinkDto != null) {
			Drink drink = drinkDao.find(drinkDto.getDrinkId());
			if (drink != null) {
				drink.setDrinkTitle(drinkDto.getDrinkTitle());
				drink.setDrinkImage(drinkDto.getDrinkImage());
				drink.setOldPrice(drinkDto.getOldPrice());
				drink.setNewPrice(drinkDto.getNewPrice());
				drink.setDescription(drink.getDescription());
				drink.setUpdateTime(new Date(DateUtils.now()));
				drink.setUpdateId("ZUchihashishui");
				drinkDao.save(drink);
			}
		}

	}

	@Override
	public List<DrinkDto> searchDrink(DrinkDto drink) {
		List<DrinkDto> list = new ArrayList<>();
		if (drink != null) {
			DrinkDto dto = null;
			List<Object[]> listObj = drinkDao.searchDrink(
					drink.getRestaurantId(), drink.getMenuDrinkId(),
					drink.getDrinkId(), drink.getDrinkName(),
					drink.getDrinkTitle(), drink.getOldPrice(),
					drink.getNewPrice());
			for (Object[] obj : listObj) {
				dto = new DrinkDto();
				dto.setRestaurantId((Integer) obj[0]);
				dto.setRestaurantName((String) obj[1]);
				dto.setMenuDrinkId((Integer) obj[2]);
				dto.setMenuDrinkName((String) obj[3]);
				Drink dr = (Drink) obj[4];
				dto.setDrinkId(dr.getDrinkId());
				dto.setDrinkName(dr.getDrinkName());
				dto.setDrinkTitle(dr.getDrinkTitle());
				dto.setDrinkImage(dr.getDrinkImage());
				dto.setOldPrice(dr.getOldPrice());
				dto.setNewPrice(dr.getNewPrice());
				dto.setCreateTime(dr.getCreateTime());
				dto.setUpdateTime(dr.getUpdateTime());
				dto.setCreateId(dr.getCreateId());
				dto.setUpdateId(dr.getUpdateId());
				dto.setDrinkStatus(dr.getDrinkStatus());
				dto.setDescription(dr.getDescription());
				list.add(dto);
			}
		}
		return list;
	}

}
